﻿(function ($) {
    app.modals.CreateOrEditPlanModal = function () {
        var _modalManager;

        var _planService = abp.services.app.plan;

        //$(".maxlength-handler").maxlength({
        //          limitReachedClass: "label label-danger",
        //          alwaysShow: true,
        //          threshold: 5,
        //          placement: 'bottom'
        //      });

        var _$planInformationForm = null;

        this.init = function (modalManager) {
            _modalManager = modalManager;
            _$planInformationForm = _modalManager.getModal().find("form[name=planInformationsForm]");

            //渲染select
            $("#lineIdEdit").selectpicker();

            //渲染select
            $("#alcCodeIdEdit").selectpicker();

            //渲染select
            $("#planModeIdEdit").selectpicker();

            // 初始化 计划日期 的包含时分秒的日期控件
            //包含时分秒的日期选择器
            $("input[name=PlanDate]").datepicker({
                autoclose: true,
                isRTL: false,
                format: "yyyy-mm-dd",
                pickerPosition: ("bottom-left"),
                //默认为E文按钮要中文，自己去找语言包
                todayBtn: true,
                todayHighlight: true,
                language: "zh-CN"
            });

            //设置默认显示日期
            //$("input[name=PlanDate]").datepicker('setDate', Date());
        }
        //下拉框二级联动
        $("#lineIdEdit").change(function () {
            //获取线体的ID
            var lineID = $("#lineIdEdit").val();
            //获取线体
            var lst = $("#alcCodeIdEdit");
            $.ajax({
                type: "get",
                url: "/LineAlcCodes/GetAllAlcCodesByLineId",
                data: { id: lineID },
                dataType: 'JSON', //返回数据的格式
                contentType: 'application/json',
                success: function (data, status) {
                    if (status == "success") {
                        lst.empty();//清空下拉框
                        $.each(data, function (i, item) {
                            lst.append("<option value='" + data[i].Value + "'>" + data[i].DisplayText + "</option>");
                        });
                        lst.selectpicker('refresh');
                    }
                },
                error: function () {
                    toastr.error('Error');
                },
            });
        });
        this.save = function () {
            if (!_$planInformationForm.valid()) {
                return;
            }
            //校验通过

            var plan = _$planInformationForm.serializeFormToObject();
            //  console.log(plan);

            _modalManager.setBusy(true);

            _planService.createOrUpdatePlanAsync({
                planEditDto: plan
            }).done(function () {
                //提示信息
                abp.notify.info(app.localize('SavedSuccessfully'));
                //关闭窗体
                _modalManager.close();
                //信息保存成功后调用事件，刷新列表
                abp.event.trigger('app.createOrEditPlanModalSaved');
            }).always(function () {
                _modalManager.setBusy(false);
            });
        }
    }
})(jQuery);